require(['loading'], function(loading) {
    var changeMobile = new Vue({
        el: '#changeMobile',
        data: {
            showLoading: false,
            validCode: true,
            yzmUrl: wxUrl + '/v3.2/login/verifyCode',
            changeUrl: wxUrl + '/v3.2/tuser/modifyMobile',
            yzmitem: {
                params:{
                    token: token,
                    mobile: '',
                    msgType:3
                }
            },
            changeitem: {
                params:{
                    token: token,
                    userToken:userToken,
                    newMobile: '',
                    exp:'',
                    verifyToken:'',
                    verifyCode:''
                }
            }
        },
        ready: function() {
            bindMobileKeyUp();
            bindYzmClick();
            bindSubmit();
        },
        methods: {
            getyzm: function() {
                if(!checkPhone()){
                    return;
                }
                var phone = $("#tel").val();
                var code=$(".msgs");
                changeMobile.yzmitem.params.mobile=phone;
                var time=60;
                if (this.validCode) {
                    this.validCode=false;
                    code.addClass("msgs1");
                    this.$http.get(this.yzmUrl, this.yzmitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var success = result.data.success;
                            console.log(success)
                            if(success){
                                var t=setInterval(function  () {
                                    time--;
                                    code.html(time+"s后重发");
                                    if (time==0) {
                                        clearInterval(t);
                                        code.html("重发验证码")
                                        code.css("color","#FF9500");
                                        changeMobile.validCode=true;
                                        code.removeClass("msgs1");
                                    }
                                },1000)
                                this.changeitem.params.exp = result.data.exp;
                                this.changeitem.params.verifyToken = result.data.verifyToken;
                            }else{
                                this.validCode=true;
                                code.removeClass("msgs1"); 
                            }
                        }else{
                            this.validCode=true;
                            code.removeClass("msgs1"); 
                        }
                    },function(response) {
                        //errorCallback
                    });
                }
            },
            toChangeMobile: function() {
                this.$http.get(this.changeUrl, this.changeitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.code != null && result.code == 0){
                            if(result.data!=null){
                                var success = result.data.success;
                                if(success){
                                    promptBox("更改手机号成功");
                                    setTimeout(function(){
                                        window.location.go(-1);
                                    },1000);
                                }else{
                                    promptBox("更改手机号失败，请重试");
                                }
                            }
                        }
                        
                    },function(response) {
                        //errorCallback
                    });
            }
        }
        
    });

    function bindMobileKeyUp(){
        $('.content').on('keyup',function(){
            $('.clear').show();
        });
        $('.clear').click(function(){
            $(this).hide();
            $('.content').val('');
        });
    }

    function bindYzmClick(){
        //获取短信验证码
        $(".msgs").click(function() {
            changeMobile.getyzm();
        })
    }

    function checkPhone(){
        var phone = $("#tel").val();
        if(phone == "") {
            promptBox("手机号码不能为空");
        } else {
            var re = /^[1][34587]\d{9}$/; //手机号码验证正则表达式
            if(re.test(phone)) {
                return true;
            } else {
                promptBox("请输入正确的手机号码");
            }
        }
        return false;
    }

    function bindSubmit(){
        $("#submit").click(function() {
            if(checkPhone()){
                var yzm = $("#yz").val();
                if(yzm == ''){
                    promptBox("请输入验证码");
                    return;
                }
                changeMobile.changeitem.params.verifyCode=yzm;
                var phone = $("#tel").val();
                changeMobile.changeitem.params.newMobile=phone;
                changeMobile.toChangeMobile();
            }
        });
    }
})

// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
	goBackVHistory();
});	